<?php

/**
 * 后台用户相关model
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;

class Imgs extends ActiveRecord
{
    
    /**
     * 获取图片列表
     */
    public function getImageList( $params = array() )
    {
        $img = self::find();
        $img->select('*');
        $page = $params['page'];
        $offset = 0;
        $limit = $params['pageSize'];
        if($page > 0){
            $offset = ($page - 1) * $limit;
        }
        $img->where('status = 1');
        $count = $img->count();
        $img->offset( $offset )->limit( $limit );
        $result = $img->asArray()->all();
        $res['data'] = $result;
        $res['total'] = $count;
        return $res;
//        $data['dataProvider'] = new ArrayDataProvider([
//            'allModels'=> $result,
////            'sort' => [
////                'attributes' => ['userId', 'createTime', 'sex', 'nickname','lastTime'],
////            ],
//        ]);
//        $data['pagination'] = new Pagination([
////            'pageSize'          => $limit,
//            'defaultPageSize'   => $limit,
//            'totalCount'        => $count,
//        ]);
//        return $data;
    }
    
    /**
     * 获取图片列表
     */
    public static function getAllImage( $params = array() )
    {
        $img = self::find();
        $img->select('*');
        $img->where('status = 1');
        $result = $img->asArray()->all();
        return $result;
    }
    
    /**
     * 删除图片
     */
    public function delImages($ids)
    {
        if(empty($ids)){
            return '请选择图片';
        }
        $res = self::updateAll(['status'=>0],"id in ({$ids})");
        return $res;
    }
    
    /**
     * 新增图片
     */
    public static function AddImages( $paths = [] )
    {
        if(empty($paths)){
            return false;
        }
        $values = [];
        $now = date('Y-m-d H:i:s');
        $field = ['img_url','status','create_time'];
        foreach($paths as $v){
            $values[] = [
                'img_url' => $v,
                'status'  => 1,
                'create_time' => $now,
            ];
        }
        $res = \Yii::$app->db->createCommand()->batchInsert('xs_imgs', $field, $values)->execute();
        return $res;
    }
    
}

